//
//  ErrorCatchManager.m
//  yqApp
//
//  Created by dqb on 16/7/4.
//  Copyright © 2016年 yq. All rights reserved.
//

#import "APErrorCatchManager.h"

@implementation APErrorCatchManager

+ (APErrorCatchManager *)manager{
    static id shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[[self class] alloc] init];
    });
    return shareInstance;
}

- (KNSURLError)getErrorCode:(NSError *)error{
    return error.code;
}

- (NSError *)getError:(NSError *)error{
    KNSURLError errorcode = [self getErrorCode:error];
    NSError *errorDescription = nil;
    switch (errorcode) {
        case KNSURLErrorBadURL:
            errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":@"Bad URL"}];
            break;
        case KNSURLErrorTimedOut:
            errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":@"request time out"}];
            break;
        case KNSURLErrorFileDoesNotExist:
            errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":@"file is not exist"}];
            break;
        case KNSURLErrorCannotFindHost:
        case KNSURLErrorCannotConnectToHost:
            errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":@"no network"}];
            break;
        case KNSURLErrorNotConnectedToInternet:
            errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":@"no network"}];
            break;
        case KNSURLErrorNetworkConnectionLost:
            errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":@"no network"}];
            break;
        case KNSURLErrorBadServerResponse:
            errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":@"no network"}];
            break;
        default:
            if (error.domain) {
                errorDescription = [NSError errorWithDomain:error.domain code:error.code userInfo:@{@"message":error.localizedDescription}];
            }else{
                errorDescription = [NSError errorWithDomain:@"" code:error.code userInfo:@{@"message":error.localizedDescription}];
            }
             break;
    }
    return errorDescription;
}

@end
